import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;


public class UDPConnectionManager implements IMasterConnection{

	DatagramSocket ds = null;
	public final static int PORT = 8765;
	
	public UDPConnectionManager() 
	{
		try {
			ds = new DatagramSocket();
		} 
		catch (SocketException e) {
			e.printStackTrace();
		}
	}
	@Override
	public void sendData(IMessage message, IMachine machine) {
		byte[] serializedData = message.serialize();
		try {
			DatagramPacket dp = new DatagramPacket(serializedData, serializedData.length, InetAddress.getByName(machine.getIP()),PORT);
			ds.send(dp);
		} 
		catch (UnknownHostException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
	}	

}
